package com.cxz.study.domain.order;

import com.cxz.study.domain.exception.InventoryNotEnoughException;
import com.cxz.study.domain.exception.OrderNotCancelableException;
import com.cxz.study.domain.order.orderId.OrderId;
import com.cxz.study.domain.order.service.InventoryService;
import com.cxz.study.domain.util.Asserts;
import lombok.Value;

import java.time.LocalDateTime;

/**
 * @className: Order
 * @author: ocxz1
 * @date: 2025/6/13 下午6:49
 * @Version: 1.0
 * @description: ddd-struct3
 */
@Value
public class Order {
    OrderId id;
    Long userId;
    Long productId;
    Integer productCount;
    LocalDateTime createdAt;
    OrderStatus status;

    /**
     * 取消订单
     * @return 返回订单
     */
    public Order cancel() {
        Asserts.isTrue(status == OrderStatus.CREATED, OrderNotCancelableException.class);
        return new Order(id, userId, productId, productCount, createdAt, OrderStatus.CANCELED);
    }

    /**
     * 检查库存
     * @param inventoryService 库存服务
     */
    public void checkInventory(InventoryService inventoryService) {
        Asserts.isTrue(inventoryService.isEnough(productId, productCount),
                InventoryNotEnoughException.class);
    }
}
